package com.kk.nb_car.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kk.nb_car.exception.TooManyCarNumException;
import com.kk.nb_car.remote.UserService;
import com.kk.nb_car.service.CarService;
import com.kk.nb_car.mapper.CarMapper;
import com.kk.nb_car.bean.Car;
import com.woniuxy.boot.common.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: K
 * @Date: 2024/12/13
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Autowired
    private CarMapper carMapper;
    @Autowired
    private UserService userService;
    /**
     * 用户添加车辆信息
     *
     * @param car
     */
    @Override
    @Transactional
    public void addCar(Car car) {
        //查询用户的绑定数量是否达到上限，5辆
        UserDTO user = userService.getUserById(car.getUserId());
        System.out.println("userId==>"+user.getId());
        if (user.getCarNum()>=5) {
            //如果达到上限就抛异常
            throw new TooManyCarNumException("绑定车辆达到上限！");
        }
        //数据库增加车辆信息
        carMapper.insert(car);
        //用户的车辆绑定数量+1
        userService.carNumPlusOne(car.getUserId());
    }
}

